// 新版--子级占比/排名卡
var numberFormate = iot.getNumberFormat("0.00");
var energyDevId = {
    "collectChart": {
        id:10339,
        unitTotal:"T",
        unitSingle:"kg/辆",
        titleTotal:"总碳排子级占比排名",
        titleSingle:"单车碳排子级排名"
    },//碳排
    "costPools": {
        id:10330,
        unitTotal:"千元",
        unitSingle:"元/辆",
        titleTotal:"总成本子级占比排名",
        titleSingle:"单车成本子级排名"
    },//成本
    "summarySteams": {
        id:10326,
        unitTotal:"T",
        unitSingle:"T/辆",
        titleTotal:"总蒸汽子级占比",
        titleTotal1:"总蒸汽子级排名",
        titleSingle:"单车蒸汽子级排名"
    },//蒸汽
    "aggregateWaters": {
        id:10318,
        unitTotal:"T",
        unitSingle:"T/辆",
        titleTotal:"总用水子级占比",
        titleTotal1:"总用水子级排名",
        titleSingle:"单车用水子级排名"
    },//用水
    "aggregateGass": {
        id:10311,
        unitTotal:"Nm³",
        unitSingle:"Nm³/辆",
        titleTotal:"总燃气子级占比",
        titleTotal1:"总燃气子级排名",
        titleSingle:"单车燃气子级排名"
    },//燃气
    "aggregateElectricitys": {
        id:10304,
        unitTotal:"kwh",
        unitSingle:"kwh/辆",
        titleTotal:"总用电子级占比",
        titleTotal1:"总用电子级排名",
        titleSingle:"单车用电子级排名"
    },//用电
    "airCollectionss": {
        id:10295,
        unitTotal:"m³",
        unitSingle:"m³/辆",
        titleTotal:"总空气子级占比",
        titleTotal1:"总空气子级排名",
        titleSingle:"单车空气子级排名"
    },//空气
    "aggregateCoolings": {
        id:10287,
        unitTotal:"Kcal",
        unitSingle:"Kcal/辆",
        titleTotal:"总冷量子级占比",
        titleTotal1:"总冷量子级排名",
        titleSingle:"单车冷量子级排名"
    }//冷量
};

dev.action = function(resp,param){

    var db = iot.db();
    var name = param.name

    var workSpaceId = iot.getEnergySpaceId(param.workSpaceName, param.name);
    var param= {
        id:energyDevId[param.name].id,
        time:param.time,
        selectType:param.selectType,
        workSpace:workSpaceId,
        workSpaceName: param.workSpaceName
    }
    var jsData = iot.bridgeOtherJsResult(JSON.stringify(param));
    var proportData = jsData.jsData;// 占比数据
    var rankData = iot.list();// 排名数据
    var singleData = {
        name : iot.list(),
        value : iot.list(),
        rank : iot.list()
    };// 单车数据

    var datas = proportData.datas;

    // JS调用Java排序方法，根据value进行降序排列
    var Collections = Java.type("java.util.Collections");
    var Comparator = Java.type("java.util.Comparator")
    var Printer = Java.extend(Comparator, {
        compare : function (o1, o2) {
            var value1 = Number(o1.value);
            var value2 = Number(o2.value);
            if (value1 > value2) {
                return -1;
            }
            if (value1 > value2) {
                return 1;
            }
            return 0;
        }
    })
    Collections.sort(datas, new Printer());
    var totalSum = 0; // 总数
    for (var index in datas) {
        var value = Number(datas[index].value);
        totalSum += value;
    }
    for (var index in datas) {
        var value = datas[index].value;
        var proport = totalSum !== 0 ? value / totalSum : 0; // 占比
        datas[index].proport = numberFormate.format(proport * 100) + "%";
        datas[index].rank = (index + 1);
    }
    rankData.add(datas);


    var spaces = param.workSpaceName.split('/');
    var factoryName = spaces[spaces.length-1]
    //获取模板
    var templateFramework = db.qry("SELECT es2.* FROM \n" +
        "`energy_struc` es1 LEFT JOIN `energy_struc` es2 on es1.id = es2.pid\n" +
        "where es1.name = '"+ spaces[spaces.length-1] +"' and es1.type = \"模板\";");

    var datasLength = datas.length + 1;

    if (datas.length === 0){

        templateFramework.forAll(function (item) {
            var rankDataItem = {
                name: "",
                proport: "0.00%",
                rank: datasLength,
                value: "0.00",
            }
            rankDataItem.name = item.name;
            datas.add(rankDataItem);
        })
    }else {
        templateFramework.forAll(function (item) {

            var rankDataItem = {
                name: "",
                proport: "0.00%",
                rank: datasLength,
                value: "0.00",
            }
            var isAdd = true;
            for (var index in datas) {
                if (item.name === datas[index].name) {
                    isAdd = false;
                }

                if (isAdd) {
                    rankDataItem.name = item.name;
                }
            }
            if (isAdd) {
                datas.add(rankDataItem);
            }

        })
    }


    //start计算单车数据
    var totalYield = 0
    var pvYield;
    //获取产量
    if (factoryName !== 'PV') {
        var yieldId = getFactoryYieldId(factoryName)

        var yieldResult = getFactoryYield(yieldId, param.time)

        totalYield = yieldResult.totalSum;
    }else {
        pvYield = db.qry("select SUM(dvalue) totalSum, dev_id devId from iot_dev_data_win\n" +
            "where dev_id in (278, 11024, 11129, 11201, 11165)\n" +
            "and the_time like '"+ param.time +"%' \n" +
            "GROUP BY devId");
    }

    var tempData = iot.list();
    for (var index in datas) {
        var tempDataItem = {
            name : '',
            value : ''
        }
        if (factoryName === 'PV') {
            pvYield.forAll(function (yieldItem) {
                if (factoryYield[factoryHomologous[datas[index].name]] === yieldItem.devId){
                    totalYield = yieldItem.totalSum;
                }
            })
        }
        if (totalYield && totalYield !== 0) {
            tempDataItem.value = datas[index].value / totalYield;
        } else{
            tempDataItem.value = 0;
        }
        tempDataItem.name = datas[index].name
        tempData.add(tempDataItem)
    }

    Collections.sort(tempData, new Printer());
    for (var index in tempData) {
        singleData.value.add(tempData[index].value);
        if (index > 0 && singleData.value.get(index - 1) === singleData.value.get(index)) {
            singleData.rank.add(singleData.rank.get(index - 1));
        } else{
            singleData.rank.add(index + 1);
        }
        singleData.name.add(parseInt(singleData.rank.get(index)) + " " + tempData[index].name);
    }
    //end计算单车数据

    // 单车数据保留两位小数
    for (var index in singleData.value) {
        singleData.value[index] = decimalRetainRule(singleData.value[index])
    }

    for (var index in datas) {
        if (name === "collectChart" || name === "costPools") {
            datas[index].value = decimalRetainRule(datas[index].value / 1000)
        } else {
            datas[index].value = decimalRetainRule(datas[index].value)
        }
    }


    //反转
    singleData.value = iot.reverse(singleData.value);
    singleData.name = iot.reverse(singleData.name);
    singleData.rank = iot.reverse(singleData.rank);


   var rankDataJson = {
       rankData:datas
       ,
       unit : energyDevId[name].unitTotal
       ,
       title : energyDevId[name].titleTotal
       ,
       title1 : energyDevId[name].titleTotal1
   }
   var singleDataJson = {
       singleData : singleData
       ,
       unit : energyDevId[name].unitSingle
       ,
       title : energyDevId[name].titleSingle
   }

    var jsData = {
        proportData:proportData,
        rankData:rankDataJson,
        singleData: singleDataJson
    }
    resp.setJsData(jsData);
}